package com.sth0409.foldhoverrecyclerapplication;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private RecyclerView review;
    private TextView head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    class HeadBean {
        public String title;
        public List<String> item;
        public boolean isOpened;
    }


    class AdapterHeadItem extends BaseQuickAdapter<String, BaseViewHolder> {
        public AdapterHeadItem(@Nullable List<String> data) {
            super(R.layout.item_child, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.child_title, item);
        }
    }

    class AdapterHead extends BaseQuickAdapter<HeadBean, AdapterHead.AdapterHeadViewHolder> {
        public AdapterHead(@Nullable List<HeadBean> data) {
            super(R.layout.item_head, data);
        }

        @Override
        protected void convert(AdapterHeadViewHolder helper, HeadBean item) {
            helper.head_title.setText(item.title);

            helper.head_review.setLayoutManager(new LinearLayoutManager(mContext));
            helper.head_review.setAdapter(new AdapterHeadItem(item.item));
            if (item.isOpened) {
                helper.head_review.setVisibility(View.VISIBLE);
            } else {
                helper.head_review.setVisibility(View.GONE);
            }

        }

        public class AdapterHeadViewHolder extends BaseViewHolder {
            public View rootView;
            public TextView head_title;
            public RecyclerView head_review;

            public AdapterHeadViewHolder(View rootView) {
                super(rootView);
                this.rootView = rootView;
                this.head_title = (TextView) rootView.findViewById(R.id.head_title);
                this.head_review = (RecyclerView) rootView.findViewById(R.id.head_review);
            }

        }
    }

    private int ViewH = 0;

    private void initView() {

        review = (RecyclerView) findViewById(R.id.review);
        head = findViewById(R.id.head);

        int index = 15;
        int PersonIndex = 9;
        final List<HeadBean> headBeans = new ArrayList<>();
        for (int i = 0; i < PersonIndex; i++) {
            List<String> strings = new ArrayList<>();
            for (int i1 = 0; i1 < index; i1++) {
                strings.add("---C-------" + i1 + "---------");
            }
            HeadBean headData = new HeadBean();

            headData.title = "---P--" + i + "-----";
            headData.item = strings;
            headBeans.add(headData);

        }
        review.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        review.setLayoutManager(new LinearLayoutManager(this));
        final AdapterHead adapterHead = new AdapterHead(headBeans);
        adapterHead.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int index = 15;
                        int PersonIndex = 9;
                        final List<HeadBean> headBeanMore = new ArrayList<>();
                        for (int i = 0; i < PersonIndex; i++) {
                            List<String> strings = new ArrayList<>();
                            for (int i1 = 0; i1 < index; i1++) {
                                strings.add("---C-------" + i1 + "---------");
                            }
                            HeadBean headData = new HeadBean();

                            headData.title = "---P--" + i + "-----";
                            headData.item = strings;
                            headBeanMore.add(headData);

                        }
                        adapterHead.addData(headBeanMore);
                        adapterHead.loadMoreComplete();
                    }
                }, 1500);


            }
        });
        adapterHead.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (((AdapterHead) adapter).getData().get(position).isOpened) {
                    ((AdapterHead) adapter).getData().get(position).isOpened = false;
                } else {
                    ((AdapterHead) adapter).getData().get(position).isOpened = true;
                }

                review.scrollToPosition(position);
                adapter.notifyItemChanged(position);

            }
        });
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (int) v.getTag();

                if (adapterHead.getData().get(pos).isOpened) {
                    adapterHead.getData().get(pos).isOpened = false;
                } else {
                    adapterHead.getData().get(pos).isOpened = true;
                }
                adapterHead.notifyItemChanged(pos);


            }
        });

        review.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if ((review.getHeight() > ViewH) && head.getVisibility() == View.GONE) {
                    int l = adapterHead.getData().size();
                    for (int i = 0; i < l; i++) {
                        HeadBean headBean = adapterHead.getItem(i);
                        if (headBean.isOpened) {
                            head.setVisibility(View.VISIBLE);
                            head.setTag(i);
                            head.setText((adapterHead).getData().get(i).title);
                            break;
                        }
                    }
                }
                ViewH = review.getHeight();
            }
        });
        review.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager l = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (((AdapterHead) recyclerView.getAdapter()).getData().get(l.findFirstVisibleItemPosition()).isOpened) {

                    head.setVisibility(View.VISIBLE);
                    head.setTag(l.findFirstVisibleItemPosition());
                    head.setText(((AdapterHead) recyclerView.getAdapter()).getData().get(l.findFirstVisibleItemPosition()).title);

                } else {
                    head.setVisibility(View.GONE);
                }
            }
        });
        review.setAdapter(adapterHead);

    }

}
